//
//  CityController.m
//  敢唆嘛!
//
//  Created by lanou on 15/11/28.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "CityController.h"
#import "CityCell.h"
#import "CityModel.h"
#import "Define.h"
#import "Device.h"

@interface CityController ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic,retain)UIPickerView *picker; //创建picker视图

@property (nonatomic,retain)UITextField *address; //显示用户选中的城市
@end

@implementation CityController

{
    NSArray *_province; //省份
    NSDictionary *_city; //城市
    
}

-(void)creatCity{
    _province = @[@"北京", @"上海",@"广西", @"广东",@"黑龙江", @"吉林", @"辽宁",@"河北", @"河南", @"内蒙古",@"甘肃", @"青海", @"新疆",@"西藏", @"山西", @"陕西",@"天津", @"重庆", @"云南",@"湖南", @"湖北", @"山东",@"江苏", @"浙江", @"江西",@"福建", @"贵州", @"四川", @"海南", @"香港",@"澳门", @"台湾"];
    //市
    _city = @{
              @"北京":@[@"朝阳区", @"东城区", @"西城区"],
              @"上海":@[@"徐汇区", @"静安区", @"普陀区" , @"浦东新区", @"金山区",@"奉贤区", @"闵行区", @"闸北区", @"宝山区", @"杨浦区", @"黄浦区", @"长宁区", @"青浦区", @"松江区", @"嘉定区", @"崇明县"],
              @"广西":@[@"桂林市", @"南宁市"],
              @"广东":@[@"惠州市", @"广州市", @"深圳市",@"东莞市"],
              @"黑龙江":@[@"哈尔滨市", @"大庆市", @"牡丹江市",@"齐齐哈尔"],
              @"吉林":@[@"长春市", @"吉林市", @"通化市",@"松原市"],
              @"辽宁":@[@"沈阳市", @"大连市", @"抚顺市",@"葫芦岛市"],
              @"河北":@[@"石家庄", @"唐山市", @"秦皇岛",@"邯郸"],
              @"河南":@[@"郑州市", @"洛阳市", @"南阳市",@"平顶山市"],
              @"内蒙古":@[@"呼和浩特", @"包头市", @"鄂尔多斯",@"通辽"],
              @"甘肃":@[@"兰州市", @"酒泉市", @"天水市",@"平凉市"],
              @"青海":@[@"西宁市", @"格尔木", @"德令哈",@"玉树州"],
              @"新疆":@[@"乌鲁木齐", @"库尔勒", @"克拉玛依",@"伊宁"],
              @"西藏":@[@"拉萨市", @"日喀则市"],
              @"山西":@[@"太原市", @"临汾市", @"运城市",@"大同市"],
              @"陕西":@[@"西安市", @"咸阳市", @"延安市",@"宝鸡市"],
              @"天津":@[@"和平区", @"河东区", @"南开区",@"塘沽区"],
              @"重庆":@[@"沙坪坝区", @"涪陵区", @"渝中区",@" 江北区"],
              @"云南":@[@"昆明市", @" 曲靖市", @"玉溪市",@"大理州"],
              @"湖南":@[@"长沙市", @"岳阳市", @"常德市",@"湘潭市"],
              @"湖北":@[@"武汉市", @"宜昌市", @"襄阳市",@"黄冈市"],
              @"山东":@[@"济南市", @"青岛市", @"威海市",@"菏泽市"],
              @"江苏":@[@"南京市", @"义乌市", @"昆山市",@"江阴市"],
              @"浙江":@[@"杭州市", @"苏州市", @"无锡市",@"宁波市"],
              @"江西":@[@"南昌市", @"九江市", @"萍乡市",@"景德镇市"],
              @"福建":@[@"泉州市", @"福州市", @"厦门市",@"漳州市"],
              @"贵州":@[@"贵阳市", @"安顺市", @"都匀市",@"凯里市"],
              @"四川":@[@"成都市", @"绵阳市", @"南充市",@"泸州市"],
              @"海南":@[@"海口市", @"三亚市", @"文昌市",@"琼海市"],
              @"香港":@[@"湾仔区", @"香港岛", @"九龙",@"新界"],
              @"澳门":@[@"澳门半岛", @"离岛"],
              @"台湾":@[@"台中市", @"台北市", @"台南市",@"高雄市"],
              

              };
    
    
}

// 返回按钮触发
- (void)backBtnAction:(UIButton *)backBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //将城市添加到数组中
    [self creatCity];
    
    self.navigationItem.title = @"城市";
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建左上角返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, KSWidth / 18, KSWidth / 18);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-unie61e"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBackBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftBackBtn;
    
    //显示用户选中的城市
    self.address = [[UITextField alloc]initWithFrame:CGRectMake(KSWidth/4, 50, KSWidth/2, 50)];
    
    [self.address.layer setCornerRadius:10];
    
    self.address.textAlignment = NSTextAlignmentCenter;
    
    self.address.backgroundColor = [UIColor grayColor];
    self.address.alpha = 0.6;
    [self.view addSubview:_address];
    
    //确定按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(KSWidth/4, self.address.frame.origin.y+80, KSWidth/2, 50);
    
    [button setTitle:@"确定" forState:UIControlStateNormal];
    
    button.backgroundColor = [UIColor grayColor];
    button.alpha = 0.8;
    [button.layer setCornerRadius:10];
    
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    
    //视图
    self.picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, KSHeight - (KSHeight) / 2.5 ,KSWidth, (KSHeight - 64) / 3)];
    self.picker.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1.0];
    
    self.picker.alpha = 0.6;
    
    [self.view addSubview:_picker];
    
    self.picker.dataSource = self;
    self.picker.delegate = self;
    
    
    self.address.text = [self pickerView:self.picker titleForRow:1 forComponent:1];
    

}


#pragma mark - 该方法的返回值决定该控件包含多少列 横着分几个表
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
//    return _province.count;
    return 2;
}


#pragma mark - 该方法的返回值决定该控件指定列包含多少个列表项
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (0 == component)
    {
        return _province.count;
    }else {
        NSInteger rowProvince = [pickerView selectedRowInComponent:0];
        NSString *provinceName = _province[rowProvince];
        NSArray *citys = _city[provinceName];
        return citys.count;
    }
//    else{
//        NSInteger rowProvince = [pickerView selectedRowInComponent:0];
//        NSString *provinceName = _province[rowProvince];
//        NSArray *citys = _city[provinceName];
//        
//        NSInteger rowCity = [pickerView selectedRowInComponent:1];
//        NSString *cityName = citys[rowCity];
//        NSArray *country = _country[cityName];
//        
//        return country.count;
//    }

}

#pragma mark - 该方法返回的NSString将作为UIPickerView中指定列和列表项的标题文本
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (0 == component) {
        return _province[row];
    }
    else{
        NSInteger rowProvince = [pickerView selectedRowInComponent:0];
        NSString *provinceName = _province[rowProvince];
        NSArray *citys = _city[provinceName];
        return citys[row];
    }
    //return self.foods[component][row];
}

#pragma mark - 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(0 == component){
        [pickerView reloadComponent:1];
       // [pickerView reloadComponent:2];
    }
    if(1 == component){
     //   [pickerView reloadComponent:2];
    NSInteger rowOne = [pickerView selectedRowInComponent:0];
    NSInteger rowTow = [pickerView selectedRowInComponent:1];
  //  NSInteger rowThree = [pickerView selectedRowInComponent:2];
    
    NSString *provinceName = _province[rowOne];
    
    NSArray *citys = _city[provinceName];
    
//    NSString *cityName = citys[rowTow];
//    NSArray *countrys = _country[cityName];
    
//    NSLog(@"%@~%@", _province[rowOne], citys[rowTow]);
    
    self.address.text = [NSString stringWithFormat:@"%@-%@",_province[rowOne],citys[rowTow]];
    }
}


#pragma mark --确定修改地址的的确定键触发的方法--
-(void)buttonAction{
    
    //判断block实现 是否存在 如果不存在 相当于为nil 不进条件进行回调;
        if (self.block != nil) {
            //传值到上个界面
            self.block(self.address.text);
        }
    
        [self.navigationController popViewControllerAnimated:NO];
}


#pragma mark --触摸回弹键盘 --
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.address resignFirstResponder];
}




@end
